﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using Kovan.Model.Common;
using System.Web;
using System.Web.Mvc;


namespace Kovan.BLL.UserCenter
{
    public class BUserLogin
    {
        #region 当前类的实例
        private static readonly BUserLogin Instances = new BUserLogin();

        private BUserLogin() { }

        public static BUserLogin GetInstances()
        {
            return Instances;
        }
        #endregion
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool Login(string loginName, string pwd)
        {
            var isOk = false;
            var model = BActUser.GetInstances().GetModel(loginName, pwd);
            if (model != null)
            {
                isOk = true;
                //票证
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.name, DateTime.Now, DateTime.Now.AddDays(7), false, model.ukey);
                //用户名创建身份验证Cookie
                var authCookie = FormsAuthentication.GetAuthCookie(FormsAuthentication.FormsCookieName, false);
                authCookie.Value = FormsAuthentication.Encrypt(ticket);
                HttpContext.Current.Response.Cookies.Add(authCookie);
            }
            return isOk;
        }

        public void LoginOff()
        {
            FormsAuthentication.SignOut();
        }

        public static User GetUser()
        {
            var uModel = HttpContext.Current.Session["USER_KEY"] as User;
            if (uModel == null)
            {
                var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                var USERKEY = FormsAuthentication.Decrypt(authCookie.Value).UserData;
                uModel = GetUserInfo(USERKEY);
                HttpContext.Current.Session["USER_KEY"] = uModel;
            }
            return uModel;
        }

        public static bool SetUser(User user)
        {
            if (IsUser(user))
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["USER_KEY"] = user;
                    return true;
                }
            }
            return false;
        }

        public static User GetUserInfo(string ukey)
        {
            User user = new User();
            var uModel = BUserManage.GetInstances().GetUserModel(ukey);
            if (uModel != null)
            {
                user.LoginName = uModel.user.login_name;
                user.UserName = uModel.user.name;
                user.UserID = uModel.user.user_id;
                user.ApplicationID = uModel.user.application_id;
                user.IdentityCard = uModel.user.identity_card;
                user.DeptName = uModel.dept.name;
                user.DeptID = uModel.dept.dept_id;
                user.RoleID = uModel.role.role_id;
                user.RoleName = uModel.role.name;
                user.ApplicationID = uModel.application.application_id;
                user.ApplicationName = uModel.application.name;

                user.Menus = BUserManage.GetInstances().GetUserMenu(uModel.role.role_id.Value);
            }
            return user;
        }

        /// <summary>
        /// 检查是否正确的用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns>为null或者新new的返回false,其它的返回true</returns>
        public static bool IsUser(User user)
        {
            return (user != null && user.UserID.HasValue && user.UserID.Value > 0);
        }
    }
}
